//
//  HeadPageView.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/13.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "HeadPageView.h"

@implementation HeadPageView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)initPageViewWithViewController:(UIViewController *)viewController imagePaths:(NSArray *)imagePathAry{
    NSInteger count = imagePathAry.count;
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    self.scrollView.contentSize = CGSizeMake(width * count, height);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    //important
    self.scrollView.pagingEnabled = YES;
    
    //page
    CGFloat pageControlWidth = 10.0f *count;
    CGFloat pageControlHeight = 15.0f;
    self.pageControl.frame = CGRectMake((width-pageControlWidth)/2, height-pageControlHeight, pageControlWidth, pageControlHeight);
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = count;
    
    //图片
    
    NSInteger index = 0;
    for (NSString *imagePath in imagePathAry) {
        CGFloat xpos = index * width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xpos, 0, width, height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imagePath]];
        [self.scrollView addSubview:imageView];
        index ++;
    }
    //implement UIScrollViewDelegate
    self.scrollView.delegate = self;
}

#pragma mark -implement UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat width = self.frame.size.width;
    CGPoint offset = scrollView.contentOffset;
    self.pageControl.currentPage = offset.x/width;
}
    
- (IBAction)changePage:(id)sender {
    CGFloat width = self.frame.size.width;
    [UIView animateWithDuration:0.3f animations:^{
        NSInteger whichPage = self.pageControl.currentPage;
        self.scrollView.contentOffset = CGPointMake(width * whichPage, 0.0f);
    }];
}


@end
